/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package robocode.rescue.features;

import robocode.rescue.RobotInfo;

/**
 *
 * @author Alyson
 */
public class Defesa extends Thread{

    private RobotInfo[] robos;
    private RobotInfo[] robosInimigos;
    private int numeroDoRoboInimigo;
    private int numeroDoMeuRobo;
    
    private double posicaoAnteriorX;
    private double posicaoAnteriorY;
    
    private double[] pontoInterseccao = new double[5];
    private double[] retorno;

    public Defesa(RobotInfo[] robos, RobotInfo[] robosInimigos, int numeroDoInimigoAtacante, int numeroDoMeuRoboDefensor, double posicaoAnteriorX, double posicaoAnteriorY) {
        this.robos = robos;
        this.robosInimigos = robosInimigos;
        this.numeroDoRoboInimigo = numeroDoInimigoAtacante;
        this.numeroDoMeuRobo = numeroDoMeuRoboDefensor;
        this.posicaoAnteriorX = posicaoAnteriorX;
        this.posicaoAnteriorY = posicaoAnteriorY;
    }
    
    public void run(){
        retorno = fazCalculos();
    }
    
    public double[] getValores(){
        return retorno;
    }
    
    public double[] fazCalculos(){
        
        //System.out.println("Inimigo:" + robosInimigos[numeroDoRoboInimigo].getName() + " Meu Robo: " + robos[numeroDoMeuRobo].getName());
        double coeficienteAngular; //a
        double coeficienteLinear ; //b
        double coeficienteAngularLinhaPerpendicular;
        double coeficienteLinarLinhaPerpendicular;
        double pontoDeInterseccaoX;
        double pontoDeInterseccaoY;
        double deltaY, deltaX;
        //System.out.println("Inimigo Atual X: " + robosInimigos[numeroDoRoboInimigo].getX() + " Y:" + robosInimigos[numeroDoRoboInimigo].getY());
        //System.out.println("Inimigo Anterior X:" + posicaoAnteriorX+ " Y: " + posicaoAnteriorY);
        //System.out.println("Minha Posicao X:" +robos[numeroDoMeuRobo].getX()+ "Y: " +robos[numeroDoMeuRobo].getY());
        System.out.println("Robo: "+robosInimigos[numeroDoRoboInimigo].getName() + " velocidade: "+robosInimigos[numeroDoRoboInimigo].getVelocity());
        deltaY = posicaoAnteriorY - robosInimigos[numeroDoRoboInimigo].getY();
        deltaX = posicaoAnteriorX - robosInimigos[numeroDoRoboInimigo].getX();
        coeficienteAngular = deltaY / deltaX;
        //System.out.println("Angular: "+coeficienteAngular);
        coeficienteLinear = robosInimigos[numeroDoRoboInimigo].getY() - (coeficienteAngular * robosInimigos[numeroDoRoboInimigo].getX());
        //System.out.println("Linear: "+coeficienteLinear);

        coeficienteAngularLinhaPerpendicular = -(1 / coeficienteAngular);
        coeficienteLinarLinhaPerpendicular = robos[numeroDoMeuRobo].getY() - robos[numeroDoMeuRobo].getX() * coeficienteAngularLinhaPerpendicular;
        //System.out.println("Angular Perpendicular: "+ coeficienteAngularLinhaPerpendicular);
        //System.out.println("Linear Perpendicular: " + coeficienteLinarLinhaPerpendicular);

        pontoDeInterseccaoX = (coeficienteLinarLinhaPerpendicular - coeficienteLinear) / (coeficienteAngular - coeficienteAngularLinhaPerpendicular);
        pontoDeInterseccaoY = (pontoDeInterseccaoX * coeficienteAngularLinhaPerpendicular) + coeficienteLinarLinhaPerpendicular;
        //System.out.println("Interseccao X: "+ pontoDeInterseccaoX);
        //System.out.println("Interseccao Y: "+ pontoDeInterseccaoY);
        pontoInterseccao[0] = pontoDeInterseccaoX;
        pontoInterseccao[1] = pontoDeInterseccaoY;
        pontoInterseccao[2] = robosInimigos[numeroDoRoboInimigo].getX();
        pontoInterseccao[3] = robosInimigos[numeroDoRoboInimigo].getY();
        
        return pontoInterseccao;
    }
    
    public double getRange(double x1, double y1, double x2, double y2) {
        double x = x2 - x1;
        double y = y2 - y1;
        double range = Math.sqrt(x * x + y * y);
        return range;
    }
}
